// API 服务接口定义

import type {
  SalesOverview,
  Contract,
  Project,
  TimeSeriesData,
  ContractQueryParams,
  ProjectQueryParams,
  TimeRange,
  ApiResponse
} from '@/types'

export interface DashboardAPI {
  // 获取销售概览数据
  getSalesOverview(): Promise<ApiResponse<SalesOverview>>

  // 获取合同数据
  getContracts(params?: ContractQueryParams): Promise<ApiResponse<Contract[]>>

  // 获取项目数据
  getProjects(params?: ProjectQueryParams): Promise<ApiResponse<Project[]>>

  // 获取趋势数据
  getTrendData(metric: string, timeRange: TimeRange): Promise<ApiResponse<TimeSeriesData[]>>

  // 订阅实时数据更新
  subscribeToUpdates(callback: (data: any) => void): () => void
}

// 模拟延迟
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

// 创建成功响应
function createSuccessResponse<T>(data: T): ApiResponse<T> {
  return {
    data,
    success: true,
    timestamp: new Date()
  }
}

// 创建错误响应
function createErrorResponse<T>(message: string): ApiResponse<T> {
  return {
    data: null as any,
    success: false,
    message,
    timestamp: new Date()
  }
}

export { delay, createSuccessResponse, createErrorResponse }
